package mesweb.mesweb.controller.pm;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import mesweb.mesweb.dto.pm.MaterialEarlyDaysDto;
import mesweb.mesweb.entity.pm.mmMaterialdtl;
import mesweb.mesweb.service.pm.ImmMaterialdtlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 物料信息子表
 * @Author: jeecg-boot
 * @Date: 2021-12-26
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "物料信息子表")
@RestController
@RequestMapping("/mes/mmMaterialdtl")
public class mmMaterialdtlController {
    @Autowired
    private ImmMaterialdtlService mmMaterialdtlService;


    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam(value = "name", required = false) String name) {
        List<mmMaterialdtl> data = new ArrayList<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
            data = mmMaterialdtlService.list();
        } else {
            QueryWrapper<mmMaterialdtl> queryWrapper = new QueryWrapper<>();
            data = mmMaterialdtlService.list(queryWrapper);
        }
        return CommonResult.success(data);
    }

    /**
     * 分页列表查询
     *
     * @param mmMaterialdtl
     * @param page
     * @param size
     * @param req
     * @return
     */
    @ApiOperation(value = "物料信息子表-分页列表查询", notes = "物料信息子表-分页列表查询")
    @GetMapping(value = "/list")
    public CommonResult<?> queryPageList(mmMaterialdtl mmMaterialdtl,
                                         @RequestParam(name = "page", defaultValue = "1") Integer page,
                                         @RequestParam(name = "size", defaultValue = "10") Integer size,
                                         HttpServletRequest req) {
        QueryWrapper<mmMaterialdtl> queryWrapper = new QueryWrapper<>();
        Page<mmMaterialdtl> pagemmMaterialdtl = new Page<mmMaterialdtl>(page, size);
        IPage<mmMaterialdtl> pageList = mmMaterialdtlService.page(pagemmMaterialdtl, queryWrapper);
        return CommonResult.success(pageList);
    }

    /**
     * 编辑
     *
     * @param mmMaterialdtl
     * @return
     */
    @ApiOperation(value = "物料信息子表-编辑", notes = "物料信息子表-编辑")
    @PostMapping(value = "/save")
    public CommonResult<?> edit(@RequestBody mmMaterialdtl mmMaterialdtl) {

        boolean result = false;
        if (mmMaterialdtl.getId() == 0L) {
            QueryWrapper<mmMaterialdtl> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("sdtlmaterialcode", mmMaterialdtl.getSdtlmaterialcode());
            mmMaterialdtl mmMaterialdtl1 = mmMaterialdtlService.getOne(queryWrapper);
            if (mmMaterialdtl1 != null) {
                String msg = StrUtil.format("编号{}已经存在！", mmMaterialdtl1.getSdtlmaterialcode());
                throw new ApiException(msg);
            }
            result = mmMaterialdtlService.save(mmMaterialdtl);
        } else {
            result = mmMaterialdtlService.updateById(mmMaterialdtl);
        }
        return CommonResult.success(result);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "物料信息子表-通过id删除", notes = "物料信息子表-通过id删除")
    @DeleteMapping(value = "/delete")
    public CommonResult<?> delete(@RequestParam(name = "id", required = true) String id) {
        mmMaterialdtlService.removeById(id);
        return CommonResult.success("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "物料信息子表-批量删除", notes = "物料信息子表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public CommonResult<?> deleteBatch(@ApiParam("mmMaterialdtlIDs") @RequestBody List<Long> ids) {
        this.mmMaterialdtlService.removeByIds(ids);
        return CommonResult.success("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "物料信息子表-通过id查询", notes = "物料信息子表-通过id查询")
    @GetMapping(value = "/queryById")
    public CommonResult<?> queryById(@RequestParam(name = "id", required = true) String id) {
        mmMaterialdtl mmMaterialdtl = mmMaterialdtlService.getById(id);
        return CommonResult.success(mmMaterialdtl);
    }

    /**
     * 更新提前期
     *
     * @param materialEarlyDaysDto
     * @return
     */
    @ApiOperation(value = "物料信息子表-更新提前期", notes = "物料信息子表-更新提前期")
    @PostMapping(value = "/updateEarlyPeriod")
    public CommonResult<?> updateEarlyPeriod(@RequestBody MaterialEarlyDaysDto materialEarlyDaysDto) {
        UpdateWrapper<mmMaterialdtl> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", materialEarlyDaysDto.getDtlmaterialId());
        updateWrapper.set("nearlyDays", materialEarlyDaysDto.getEarlydays());
        boolean result = mmMaterialdtlService.update(updateWrapper);
        return CommonResult.success(result);
    }

}
